// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import infiniteScroll from 'vue-infinite-scroll'
import Api from './api'
import store from './vuex/store'

Vue.use(infiniteScroll);

router.beforeEach((to, from, next) => {
  console.log(router.options.routes.length);
  if(store.state.auth.menu==false){
    if(router.options.routes.length<=3){
      Api.getMenu().then((response)=>{
        if(response.status===200){
          var _router=[];
          for(var i=0;i<response.data.menus.length;i++){
            var route={
              path:response.data.menus[i].path,
              component:require('./components/article/List.vue'),
              name:response.data.menus[i].name,
              meta:{
                title:response.data.menus[i].title,
                menuId:response.data.menus[i].id,
              },
              hidden:false,
              children:[]
            };
            _router.push(route);
            router.options.routes.push(route);
          }
          router.addRoutes(_router);
          store.commit("SET_MENU",{menu:true});
          next();
        }
      });
    }else {
      next();
    }
  }else{
    next();
  }

});



Vue.config.productionTip = false;

Vue.filter('time', function (value) {//时间过滤器
  var now = new Date().getTime(),
    oldTime = new Date(value).getTime(),
    difference = now - oldTime,
    result='',
    minute = 1000 * 60,
    hour = minute * 60,
    day = hour * 24,
    halfamonth = day * 15,
    month = day * 30,
    year = month * 12,
    _year = difference/year,
    _month =difference/month,
    _week =difference/(7*day),
    _day =difference/day,
    _hour =difference/hour,
    _min =difference/minute;
  if(_year>=1) {result= "发表于 " + ~~(_year) + " 年前"}
  else if(_month>=1) {result= "发表于 " + ~~(_month) + " 个月前"}
  else if(_week>=1) {result= "发表于 " + ~~(_week) + " 周前"}
  else if(_day>=1) {result= "发表于 " + ~~(_day) +" 天前"}
  else if(_hour>=1) {result= "发表于 " + ~~(_hour) +" 个小时前"}
  else if(_min>=1) {result= "发表于 " + ~~(_min) +" 分钟前"}
  else result="刚刚";
  return result;
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})
